package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.CoachInterpretationAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.CoachInterpretationModel;
import com.banshuibao.bsb_android.myinfterface.RecyclerItemListener;
import com.banshuibao.bsb_android.util.IntentManager;


/**
 * Created by zyh on 2017/7/7.
 */

public class CoachInterpretationActivity extends BaseActivity {

    private RecyclerView listView;
    private CoachInterpretationAdapter adapter;
    private String uuid;
    private LinearLayout l1, l2, l3;
    private TextView title1, title2, title3;
    private View line_1, line_2, line_3;
    private int type = R.id.title_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coach_detail);
        initData();
        initListView();
        initTitle();
    }

    private void initData() {
        setTitleText("同步解读详情");
        uuid = getIntent().getStringExtra("uuid");
    }

    private void initListView() {
        listView = findViewById(R.id.listView);
        listView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CoachInterpretationAdapter(this);
        adapter.setpL(new RecyclerItemListener<CoachInterpretationModel.PolicyQas>() {
            @Override
            public void onItemClickListener(View view, CoachInterpretationModel.PolicyQas data) {
                if (data != null)
                    IntentManager.goCoachQasDetailActivity(CoachInterpretationActivity.this, data.getUuid());
            }
        });
        adapter.setrL(new RecyclerItemListener<CoachInterpretationModel.Relateds>() {
            @Override
            public void onItemClickListener(View view, CoachInterpretationModel.Relateds data) {
                if (data != null)
                    IntentManager.goCoachExpertDetailActivity(CoachInterpretationActivity.this, data.getUuid());
            }
        });
        listView.setAdapter(adapter);
        initListViewData();
    }

    private void initListViewData() {
        loadCoachData();
    }

    private void initTitle() {
        title1 = findViewById(R.id.title_text1);
        title2 = findViewById(R.id.title_text2);
        title3 = findViewById(R.id.title_text3);
        l1 = findViewById(R.id.title_1);
        l2 = findViewById(R.id.title_2);
        l3 = findViewById(R.id.title_3);
        line_1 = findViewById(R.id.line_1);
        line_2 = findViewById(R.id.line_2);
        line_3 = findViewById(R.id.line_3);
        l1.setOnClickListener(l);
        l2.setOnClickListener(l);
        l3.setOnClickListener(l);
    }

    private View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == type)
                return;
            type = v.getId();
            if (type == R.id.title_1) {
                title1.setTextColor(getResources().getColor(R.color.line_blue));
                title2.setTextColor(getResources().getColor(R.color.text_black));
                title3.setTextColor(getResources().getColor(R.color.text_black));
                line_1.setBackgroundResource(R.color.line_blue);
                line_2.setBackgroundResource(R.color.line_color);
                line_3.setBackgroundResource(R.color.line_color);
            } else if (type == R.id.title_2) {
                title1.setTextColor(getResources().getColor(R.color.text_black));
                title2.setTextColor(getResources().getColor(R.color.line_blue));
                title3.setTextColor(getResources().getColor(R.color.text_black));
                line_1.setBackgroundResource(R.color.line_color);
                line_2.setBackgroundResource(R.color.blue);
                line_3.setBackgroundResource(R.color.line_color);
            } else {
                title1.setTextColor(getResources().getColor(R.color.text_black));
                title2.setTextColor(getResources().getColor(R.color.text_black));
                title3.setTextColor(getResources().getColor(R.color.line_blue));
                line_1.setBackgroundResource(R.color.line_color);
                line_2.setBackgroundResource(R.color.line_color);
                line_3.setBackgroundResource(R.color.blue);
            }
            if (adapter != null) {
                adapter.setTitle(type);
            }
        }
    };

    @Override
    protected void onDestroy() {
        if (adapter != null) {
            adapter.clear();
            adapter = null;
        }
        super.onDestroy();
    }


    private void loadCoachData() {
        CoachInterpretationModel.Body body = new CoachInterpretationModel.Body();
        body.setType(CoachInterpretationModel.Body.SYNCHRO_TYPE);
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.POLICY_DETAILS, body, new HttpResponse(this, CoachInterpretationModel.class) {
            @Override
            public void onResult(Object obj) {
                CoachInterpretationModel model = (CoachInterpretationModel) obj;
                if (adapter != null) {
                    adapter.setExpert(model.getData().getExpert());
                    adapter.setOfficial(model.getData().getOfficial());
                    if (model.getData().getPolicyQas() != null)
                        adapter.setPolicyQases(model.getData().getPolicyQas());
                    if (model.getData().getPolicyRelateds() != null)
                        adapter.setRelateds(model.getData().getPolicyRelateds());
                    adapter.initAdapter();
                }
            }
        });
    }
}
